﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;

//管理种植地
public class PlantManager : MonoBehaviour
{
    public static PlantManager Instance { get; private set; }
    //得到瓦片地图
    public Tilemap intercatableMap;
    //得到透明瓦片
    public Tile interactaleTile;
    //得到锄地瓦片
    public Tile groundHoedTile;

    public GameObject carrot;
    public GameObject tomato;
    public GameObject flower;
    public GameObject flowers;
    public GameObject traps;

    private void Awake()
    {
        Instance = this;
    }

    private void Start()
    {

        InitIntercatable();
    }

    //修改Interactable的瓦片为不可见
    public void InitIntercatable()
    {
        //遍历所有瓦片的位置
        foreach (Vector3Int position in intercatableMap.cellBounds.allPositionsWithin)
        {
            //print(position);得到瓦片的位置
            TileBase tile = intercatableMap.GetTile(position);
            //print(tile);得到瓦片
            if (tile != null)
            {
                //替换瓦片
                intercatableMap.SetTile(position, interactaleTile);
            }

        }
    }

    //锄地方法
    public void HodeGround(Vector3 position)
    {
        //得到瓦片位置
        Vector3Int tilePosition = intercatableMap.WorldToCell(position);
        TileBase tile = intercatableMap.GetTile(tilePosition);
        if (tile != null && tile.name == intercatableMap.name)
        {
            intercatableMap.SetTile(tilePosition, groundHoedTile);
        }
    }

    //种胡萝卜方法
    public void PlantCarrot(Vector3 position, ToolbarUI toolbarUI)
    {
        //得到瓦片位置
        Vector3Int tilePosition = intercatableMap.WorldToCell(position);
        TileBase tile = intercatableMap.GetTile(tilePosition);
        if (tile != null && tile == groundHoedTile)
        {
            Instantiate(carrot, tilePosition + new Vector3(1f,0.65f,0), Quaternion.identity);
            toolbarUI.GetSelectSlotUI().GetData().Reduce();
        }
    }
    //种番茄方法
    public void PlantTomato(Vector3 position,ToolbarUI toolbarUI)
    {
        //得到瓦片位置
        Vector3Int tilePosition = intercatableMap.WorldToCell(position);
        TileBase tile = intercatableMap.GetTile(tilePosition);
        if (tile != null && tile == groundHoedTile)
        {
            Instantiate(tomato, tilePosition + new Vector3(1f, 0.65f, 0), Quaternion.identity);
            toolbarUI.GetSelectSlotUI().GetData().Reduce();
        }
    }

    //种花方法
    public void PlantFlower(Vector3 position, ToolbarUI toolbarUI)
    {
        //得到瓦片位置
        Vector3Int tilePosition = intercatableMap.WorldToCell(position);
        TileBase tile = intercatableMap.GetTile(tilePosition);
        if (tile != null && tile == groundHoedTile)
        {
            Instantiate(flower, tilePosition + new Vector3(1f, 0.65f, 0), Quaternion.identity);
            toolbarUI.GetSelectSlotUI().GetData().Reduce();
        }
    }

    //种花2
    public void PlantFlowers(Vector3 position, ToolbarUI toolbarUI)
    {
        Instantiate(flowers, position, Quaternion.identity);
        toolbarUI.GetSelectSlotUI().GetData().Reduce();
    }

    //放捕兽夹
    public void PlantTraps(Vector3 position, ToolbarUI toolbarUI)
    {
        Instantiate(traps, position, Quaternion.identity);
        toolbarUI.GetSelectSlotUI().GetData().Reduce();
    }
}
